from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.devtools.v136.fed_cm import click_dialog_button
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
import requests
import os
import time
import random


#有加载按钮的滚动加载页面爬虫
# 创建保存图片的目录
os.makedirs('unsplash_photos', exist_ok=True)

# 设置 Edge 浏览器选项
edge_options = Options()
edge_options.add_argument('--headless')  # 无头模式
edge_options.add_argument('--disable-gpu')
edge_options.add_argument('--no-sandbox')
edge_options.add_argument('--disable-dev-shm-usage')
edge_options.add_argument('--start-maximized')  # 最大化窗口确保所有元素可见
edge_options.add_argument('--log-level=3')  # 减少日志输出

# 初始化 Edge WebDriver
driver = webdriver.Edge()

try:
    # 设置请求头
    driver.execute_cdp_cmd('Network.setUserAgentOverride', {
        "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59"
    })

    print("正在访问 Unsplash...")
    # 打开目标网页
    driver.get('https://unsplash.com/s/photos/empty-room')

    # 等待页面加载
    time.sleep(3)

    # 接受cookies（如果出现）
    try:
        accept_button = WebDriverWait(driver, 5).until(
            EC.element_to_be_clickable((By.XPATH, '//button[contains(., "Accept")]'))
        )
        accept_button.click()
        print("已接受cookies")
        time.sleep(1)
    except:
        print("no cookies")

    # 模拟滚动加载更多图片
    last_height = driver.execute_script("return document.body.scrollHeight")
    scroll_attempts = 0

    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    button = driver.find_element(By.XPATH, "/html/body/div[1]/div[3]/div[2]/div[2]/div[4]/div[3]")
    button.click()
    print("click button")

    print("开始滚动加载图片...")
    while scroll_attempts < 50:  # 最多滚动10次
        # 滚动到页面底部
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        # 随机等待时间（更人性化）
        time.sleep(random.uniform(1.5, 3.5))

        # 等待新图片加载
        try:
            WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.CSS_SELECTOR, 'figure[itemprop="image"]:last-child'))
            )
        except:
            break

        # 检查页面高度是否变化
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            break
        last_height = new_height
        scroll_attempts += 1
        print(f"已滚动 {scroll_attempts} 次，当前高度: {new_height}px")

    # 定位所有图片容器
    image_containers = driver.find_elements(By.CSS_SELECTOR, 'figure[itemprop="image"]')
    print(f"找到 {len(image_containers)} 张图片")

    # 提取并下载图片
    download_count = 0
    for idx, container in enumerate(image_containers):
        try:
            # 获取图片元素
            img_element = container.find_element(By.CSS_SELECTOR, 'img[src]')

            # 获取高清图片URL
            srcset = img_element.get_attribute('srcset')
            if srcset:
                # 从srcset中选择最高清版本 (通常是最后一个)
                image_url = srcset.split(',')[-1].split()[0]
            else:
                image_url = img_element.get_attribute('src')

            # 确保URL有效
            if not image_url.startswith('http'):
                continue

            # 设置请求头（模拟浏览器）
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59',
                'Referer': 'https://unsplash.com/'
            }

            # 下载图片
            response = requests.get(image_url, headers=headers, stream=True, timeout=10)
            print('response============', response)
            # exit(0)
            if response.status_code == 200:
                # 保存图片
                filename = f"unsplash_photos/empty_room_{idx + 1}.jpg"
                with open(filename, 'wb') as f:
                    for chunk in response.iter_content(2048):
                        f.write(chunk)
                download_count += 1
                print(f"已下载: {filename} (大小: {len(response.content) // 1024}KB)")

        except Exception as e:
            print(f"下载第 {idx + 1} 张图片时出错: {str(e)}")

    print(f"\n成功下载 {download_count}/{len(image_containers)} 张图片")

finally:
    # 关闭浏览器
    driver.quit()
    print("Edge 浏览器已关闭，爬取完成！")